package com.zjdiepu.www.rest.api.yw;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.annotation.Add;
import com.zjdiepu.www.base.annotation.Update;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.ParentModel;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.common.consts.SystemConsts;
import com.zjdiepu.www.commpent.handler.TokenUserHelper;
import com.zjdiepu.www.commpent.mapper.yw.YwBusinessBuyerVoMapper;
import com.zjdiepu.www.ibiz.yw.IYwBusinessBuyerService;
import com.zjdiepu.www.model.base.dto.SysUser;
import com.zjdiepu.www.model.yw.dto.YwBusinessBuyer;

/**
 * 购车人
 * 项目名称：diepu-parent-rest <br/> 
 * 类名称：YwBusinessBuyerController <br/>  
 * 类描述： 购车人<br/>    
 * 创建人：xiongxiaotun <br/> 
 * 创建时间：2017年9月4日 上午10:54:02  <br/>   
 * 修改人：xiongxiaotun <br/> 
 * 修改时间：2017年9月4日 上午10:54:02   <br/>  
 * 修改备注：    <br/> 
 * @version V1.0   <br/>
 */
@RestController
@RequestMapping(value= "YwBusinessBuyerController", produces="application/json;charset=UTF-8", method = RequestMethod.POST)
@Api(tags = {"购车人"}, produces="application/json;charset=UTF-8")
public class YwBusinessBuyerController extends BaseController {
    @Resource
    private IYwBusinessBuyerService ywBusinessBuyerService;
    @Resource
    private YwBusinessBuyerVoMapper ywBusinessBuyerVoMapper;
    @Resource
    private TokenUserHelper tokenUserHelper;
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: getById  <br/>
     * @Description: 获取购车人 <br/>
     * @param parentModel
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "获取购车人", response = YwBusinessBuyer.class, httpMethod="POST")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = SystemConsts.MOBILE_FLAG, defaultValue="ios", value = "请求机器标识", required = true, paramType = "header", dataType = "String"),
    	@ApiImplicitParam(name = SystemConsts.HEADER_FOR_TOKEN, defaultValue="Ajesget8XF7zNMpt+MM8cr0wI8S+IO6a9giEKNouXWvOO3wWWyaKjSDTqRUtHlcNbjCg1JaPdjRX6dX/O1oGLgE=", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping("getById")
    public ApiResponse getById(@Validated(Update.class) @RequestBody ParentModel parentModel) {
    	tokenUserHelper.getUser();
    	YwBusinessBuyer ywBusinessBuyer = ywBusinessBuyerService.selectById(parentModel.getId());
    	ywBusinessBuyerVoMapper.mapper(ywBusinessBuyer);
    	return ApiResponse.success(ywBusinessBuyer);
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: saveBuyer <br/>
     * @Description: 保存购车人<br/>
     * @param parentModel
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "保存购车人", response = String.class, httpMethod="POST")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = SystemConsts.MOBILE_FLAG, defaultValue="ios", value = "请求机器标识", required = true, paramType = "header", dataType = "String"),
    	@ApiImplicitParam(name = SystemConsts.HEADER_FOR_TOKEN, defaultValue="Ajesget8XF7zNMpt+MM8cr0wI8S+IO6a9giEKNouXWvOO3wWWyaKjSDTqRUtHlcNbjCg1JaPdjRX6dX/O1oGLgE=", value = "token", required = true, paramType = "header", dataType = "String")
    })
    @RequestMapping("saveBuyer")
    public ApiResponse saveBuyer(@Validated(Add.class) @RequestBody YwBusinessBuyer ywBusinessBuyer) {
    	SysUser user = tokenUserHelper.getUser();
    	ywBusinessBuyer.setModifier(user.getId()); //统一使用修改人和修改人名称来初始化
    	ywBusinessBuyer.setModifierName(user.getName());
    	String id = ywBusinessBuyer.getId();
    	if(StringUtils.isNotBlank(id)) {
    		ywBusinessBuyerService.saveUpdateAndDelete(ywBusinessBuyer, OperateType.edit);
    	} else {
    		ywBusinessBuyerService.saveUpdateAndDelete(ywBusinessBuyer, OperateType.add);
    	}
        return ApiResponse.success(ywBusinessBuyer.getId());
    }

}
